//
//  AppDelegate.swift
//  myWeibo
//
//  Created by 樊健荣 on 16/7/18.
//  Copyright © 2016年 樊健荣. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置导航栏额皮肤
        setupAppearance()
        ///初始化window
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        
        //指定根控制器
//        let rootVC = JRMainTabBarController()
        self.window?.rootViewController = defaultViewController();
        self.window?.makeKeyAndVisible()
        
        
        
        return true
    }
    /// 类方法给外面的人调用来切换界面
    class func switchRootViewController(controller: UIViewController) {
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        appdelegate.window?.rootViewController = controller
        
    }

    private func setupAppearance() {
        let navibar = UINavigationBar.appearance()
        navibar.tintColor = UIColor.orangeColor()
    }
    
    /// 进入程序根据条件直接进入某个控制器
    private func defaultViewController() -> UIViewController {
        // 判断是否登录
        if !JRAccountViewModel.sharedAccountViewModel.isLogin {
            // 没有登录
            return JRMainTabBarController()
        } else {
            // 登录了
            let newVersion = isNewVersion()
            return newVersion ? JRNewFeatureCollectionViewController() : JRWelcomeViewController()
        }
    }
    
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取程序当前的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 获取上次运行的版本
        let key = "lastVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(key)
        
        let isNewVersion = currentVersion != lastVersion
        // 比较
        if isNewVersion {
            // 新版本
            // 保存新版本
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 返回
        return isNewVersion
    }


}

